const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');
var tab = require('../../../temple/tab/tab.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoContext: null,
    hidevido: true,
    videoSrc: '',
    data: []
  },
  dianzhan: function (e) {
    var $this = this;
    var id = njzxw.getDataVal(e, 'id');
    var type = njzxw.getDataVal(e, 'type');
    var num = njzxw.getDataVal(e, 'num');
    var isDianji = 0;
    if(type == 1){
      isDianji = $this.data.list[num].is_praise_num;
    }else if(type == 2){
      isDianji = $this.data.list[num].is_play_num;
    }else{
      isDianji = $this.data.list[num].is_collection_num;
    }
    if (isDianji == 1 && type == 2){
      return;
    }
    njzxw.ajax({
      url: url.resourcesInfoFront.dianzhan,
      load: true,
      data: {
        id: id,
        type: type,
        res_type:1
      },
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            var msg = (type == 1 ? '点赞' : type == 2 ? '播放' : '收藏') + '成功';
            if (isDianji == 1){
              msg = "取消成功";
            } 
            wx.showToast({
              title: msg,
            })
            var list = $this.data.list;
            if(type == 1){
              if (isDianji == 1) {
                list[num].praise_num = list[num].praise_num - 1;
                list[num].is_praise_num = 0;
              }else{
                list[num].praise_num = list[num].praise_num + 1;
                list[num].is_praise_num = 1;
              }
            }else if(type == 2){
              if (isDianji == 1) {
                list[num].play_num = list[num].play_num - 1;
                list[num].is_play_num = 0;
              } else {
                list[num].play_num = list[num].play_num + 1;
                list[num].is_play_num = 1;
              }
            }else{
              if (isDianji == 1) {
                list[num].collection_num = list[num].collection_num - 1;
                list[num].is_collection_num = 0;
              } else {
                list[num].collection_num = list[num].collection_num + 1;
                list[num].is_collection_num = 1;
              }
            }
            $this.setData({
              list: list
            });
          } else {
            njzxw.alert({ msg: b.data.msg });
          }
        } else {
          njzxw.alert({ msg: '操作失败' });
        }
      }
    });
  },
  /**
     * 空值视频播放器的显示
     */
  showVideo: function (e) {
    console.log(e);
    try{
      this.data.videoContext.stop();
    }catch(e){}
    var num = njzxw.getDataVal(e, 'num');
    console.log(num,this.data.list[num]);
    var src = this.data.list[num].res_path;
    if (src == undefined || src == '') {
      src = this.data.list[num].url;
    }
    this.setData({
      hidevido: false,
      videoSrc: src
    });
    this.data.videoContext.play();

    // e.currentTarget.dataset
    this.dianzhan(e);
  },
  /**
   * 空值视频播放器的显示
   */
  hideVideo: function () {
    this.setData({
      hidevido: true
    });
    this.data.videoContext.pause();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    njzxw.initFun(this);
    tab.init(this, [
      {
        name: '推荐',
        url: '/pages/shiping/shiping',
        active: false
      },
      {
        name: '视频',
        url: '/pages/shiping/tuijian/tuijian',
        active: true
      },
      {
        name: '音频',
        url: '/pages/shiping/yinping/yinping',
        active: false
      }
    ]);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.videoContext = wx.createVideoContext('vido');
    this.loadDatas(this);
  },
  loadDatas:function($this){
    njzxw.loadStart(this);
    this.loadData(this);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  loadData: function ($this) {
    njzxw.loadData($this, url.resourcesInfoFront.queryPage, {
      pageSize: 20,
      res_type:1
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadDatas(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData(this);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})